package script;

import api.UserApi;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.AssertUtil;
import utils.DBUtils;
import utils.ResponseData;
import utils.TestDataUtil;

import java.io.IOException;
import java.util.Map;

public class TestUpdateUserPwd {
    private UserApi userApi = new UserApi();
    private static final Logger logger = LoggerFactory.getLogger(TestUpdateUserPwd.class);

    @DataProvider
    public Object[][] dataBuild() throws IOException {
        Object[][] buildData = TestDataUtil.buildData("UpdateUserPwd.json", "id,pwd,rePwd,pwd_attach,statusCode,status,description");
        logger.info(String.valueOf(buildData));
        return buildData;
    }


    @Test(dataProvider = "dataBuild")
    public void testUpdateUserPwd(String id, String pwd, String rePwd, String pwd_attach, int statusCode,
                                  int status, String desc) throws Exception {
//        String id = "292";
//        String pwd = "zzh123456";
//        String rePwd = "zzh123456";
//        String pwd_attach = "2JvDd3394E";
        if (StringUtils.isNotBlank(id) && !"0".equals(id)) {
            pwd_attach = this.getPwdAttach(id);
        }
        logger.info("pwd_attach====={}", pwd_attach);
        //发送请求
        ResponseData responseData = userApi.updateUserPwd(id, pwd, rePwd, pwd_attach);
        //断言
        AssertUtil.commonAssert(responseData, statusCode, desc, status);
    }
    private String getPwdAttach(String userId) throws Exception {
        String sql = "SELECT id,pwd_attach FROM `mb_member` WHERE id=" + userId;
        Map<String, Object> result = DBUtils.getOne(sql);
        return (String) result.get("pwd_attach");
    }
}
